var message = '检测到系统当前未使用268云服务，因此云服务模块不可使用，如要使用，请关闭已有视频云服务，自行申请开通268云服务即可!';
$(function () {
    if(vlasProviderMap.type !='96K'&&vlasProviderMap.isK96keVideo!="ON"){
        icModale(1,message)
    }
    allChickBox($('.i-checks'),selectCall);
    if(registrationService){
        if(!isOpen){
            putService();
        }
    }
    if(isOpen){
        function watchBaseCount(callback) {
            $.ajax({
                url:baseloaction +'/admin/count/watchBaseCount',
                type:'post',
                dataType:'json',
                success:function (result) {
                    callback(result.entity);
                }
            });
        }
        watchBaseCount(function (data) {
            var obj = data.v96kData;
            $("#service_endTime").text(UnixToDate(obj.service_endtime));
           var serviceDay = Math.round((obj.service_endtime-new Date().getTime()/1000)/60/60/24);
           if(serviceDay>0){
               $("#service_day").text(serviceDay);
           }else{
               $("#service_day").text("服务以到期");
               $("#service_day").next().remove();
           }

            var use_flow = obj.use_flow;//已使用流量
            use_flow = bToG(use_flow);//换成G
            $("#use_flow").text(parseFloat(use_flow).toFixed(2));
            var flow = obj.flow;//总流量
            flow = bToG(flow);//换成G
            if(obj.use_flow>=obj.flow){
                $("#flow_Tips").removeClass("hide");
            }
            $("#flow").text("G，共 "+parseFloat(flow).toFixed(2)+" G");
           var  use_flow_scale =parseFloat((use_flow/flow*100)).toFixed(2)+"%";
            $("#use_flow_scale").css("width",use_flow_scale).text(use_flow_scale);
            var use_space = obj.use_space;//已使用空间
            use_space = bToG(use_space);//换成G
            $("#use_space").text(parseFloat(use_space).toFixed(2));
            var space = obj.space;//总空间
            space = bToG(space);//换成G
            $("#space").text("G，共 "+parseFloat(space).toFixed(2)+" G");
            var  use_flow_scale =parseFloat((use_space/space*100)).toFixed(2)+"%";
            $("#use_space_scale").css("width",use_flow_scale).text(use_flow_scale);
            if(obj.use_space>=obj.space){
                $("#flow_Tips").removeClass("hide");
            }
            //把B转换成G
            function bToG(dataNum) {
                var _dataNum = bToM(dataNum) / 1024;//换成G
                return _dataNum;
            }
            //把B转换成G
            function bToM(dataNum) {
                var _dataNum = parseInt(dataNum) /1024 /1024;//换成M
                return _dataNum;
            }
        });


    }
    $("body").on("click","#Get_SMS",function () {//发送短信
      var mobile =  $("input[name='mobile']").val();
        if(mobile==null||mobile==''){
            icModale(0, "请填写手机号");
            return false;
        }
        var that =$(this);
        that.attr("disabled",true);
        var time = 60;
        var inter = setInterval(function () {
            time --;
            if(time<=0){
                window.clearInterval(inter);
                that.text('获取验证码');
                that.attr("disabled",false);
            }else{
                that.text('('+time+'s)获取验证码');
            }
        },1000);
        $.ajax({
            url: baseloaction + '/admin/course/sendMobileCodeCloud',
            data: {'mobile': mobile},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                icModale(0, response.message);
            },
            error: function () {
                icModale(0, "系统错误，发送短信失败");
            }
        });
    }).on("click","#submitApplication",function(){
        putService();
        }
    ).on("click","#upgradeService",function(){
        if(videoType !='96K'){
            icModale(1,message);
        }else{
            parent.layer.open({
                area: winArea,
                btn: ['关闭'],
                content:baseloaction+"/admin/course/openUpdateServicePage",
                maxmin: true,
                shade: [0.3, '#000'],
                shadeClose: false,
                title: '升级云服务',
                type: 2,
                success:function(layer,index){
                  var that =  $(layer)

                },
                yes: function (index) {
                    parent.layer.close(index);
                }
            });
        }
    });

    function allChickBox(str, selectCall) {
        $(str).on("ifClicked", function (event) {
            selectCall(event.target);
        });
    }


    function  putService(){

        var mobile =  $("input[name='mobile']").val();
        if(mobile==null||mobile==''){
            icModale(0, "请填写手机号");
            return false;
        }
        if(!registrationService){
            var mobileCode =  $("input[name='mobileCode']").val();
            if(mobileCode==null||mobileCode==''){
                icModale(0, "请填写验证码");
                return false;
            }
        }
        var serviceNum = 0;
        var type =0;
        $.each($('#serviceType input:checkbox'),function(){
            if(this.checked){
                serviceNum+=1;
                type =$(this).val();
            }

        });
        if(serviceNum==2){
            type =3;
        }
        if(serviceNum==0){
            icModale(0, "请选择要开通的服务");
            return false;
        }
        var serviceType =  $("#submitApplication").data("type");
        var params = {"mobileNum":mobile,
            "mobileCode":mobileCode,
            "serviceType":serviceType,
            "operation": type,
            "requestType":registrationService
        };
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/admin/course/openVideoCloudService',
            type:"post",
            dataType:'json',
            data:params,
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(!registrationService){
                    var sta =result.entity;
                    if(sta!=null||sta==''){
                        if(sta=="0"){
                            if (serviceType==2) {
                                icModale(0,"您的申请以提交我们会尽快为您处理");
                            }else{
                                icModale(0,"信息获取成功");
                            }
                            setTimeout(function(){
                                if(serviceType==2){
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index);
                                }else{
                                    location.reload();
                                }

                            },1000);

                        }else if(sta=="10010"){
                            var context = '<div class="text-center pt10 mt20 pl20 pr20">' +
                                '<p><span class="fs16">您的开通提交申请已收到</br>平台将尽快核对处理开通，请稍等</span></p>'+
                                '</div>';
                            parent.layer.confirm(
                                context,
                                {
                                    type: 1,
                                    title:'友情提示：',
                                    area: ['380px'],
                                    offset: 'auto',
                                    closeBtn:0,
                                    btn:['知道了！'],
                                    btnAlign : 'c'
                                },
                                function (index) {
                                    parent.layer.closeAll();
                                    getRedirectMenu(baseloaction+"/admin/course/serviceConfig",false)
                                });
                        }
                    }else{
                        icModale(0,result.message);
                    }
                }
            },
            error:function (error) {
                icModale(0, "系统错误，提交失败");
            }
        });

    }
});



function selectCall(em) {
    var name = em.name;
    if(name=="concurrentLive") {
        var tb1 = $(document.getElementById('concurrentLive'));
        if (em.checked == false) {//未选中
            tb1.removeClass('hide');
            $("input[name='concurrentLive']").val(true);
        } else {//选中
            tb1.addClass('hide');
            $("input[name='concurrentLive']").val(false);
            tb1.find('.auth_id').val("");
            tb1.find('.auth_key').val("");
        }
    }

    if(name=="personTimeLive") {
        var tb1 = $(document.getElementById('personTimeLive'));
        if (em.checked == false) {//未选中
            $("input[name='personTimeLive']").val(true);
            tb1.removeClass('hide');
        } else {//选中
            $("input[name='personTimeLive']").val(false);
            tb1.addClass('hide');
            tb1.find('.auth_id').val("");
            tb1.find('.auth_key').val("");
        }
    }

    if(name=="interactiveLive") {
        var tb1 = $(document.getElementById('interactiveLive'));
        if (em.checked == false) {//未选中
            $("input[name='interactiveLive']").val(true);
            tb1.removeClass('hide');
        } else {//选中
            $("input[name='interactiveLive']").val(false);
            tb1.addClass('hide');
            tb1.find('.auth_id').val("");
            tb1.find('.auth_key').val("");
        }
    }

}

function UnixToDate(unixTime, isFull, timeZone) {
    if (typeof (timeZone) == 'number')
    {
        unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
    }
    var time = new Date(unixTime * 1000);
    var ymdhis = "";
    ymdhis += time.getUTCFullYear() + "-";
    ymdhis += (time.getUTCMonth()+1) + "-";
    ymdhis += time.getUTCDate();
    if (isFull === true)
    {
        ymdhis += " " + time.getUTCHours() + ":";
        ymdhis += time.getUTCMinutes() + ":";
        ymdhis += time.getUTCSeconds();
    }
    return ymdhis;
}
